// 检测app是否开启了通知权限
export const enabledPermission = () => {
  let main = plus.android.runtimeMainActivity();
  let NotificationManagerCompat = plus.android.importClass('android.support.v4.app.NotificationManagerCompat');
  //android.support.v4升级为androidx
  if (NotificationManagerCompat == null) {
    NotificationManagerCompat = plus.android.importClass('androidx.core.app.NotificationManagerCompat');
  }
  let areNotificationsEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled();
  // 未开通‘允许通知'权限，则弹窗提醒开通，并点击确认后，跳转到系统设置页面进行设置
  if (!areNotificationsEnabled) {
    plus.push.createMessage('', '', '');
  }
};

// 推送消息
export const sendMessage = (title, msg) => {
  let options = {
    cover: true, //覆盖上次，只保留最后一个
    when: new Date(),
    sound: 'system',
    title: title
  };
  let body = {
    id: 'id',
    key: 'key'
  };
  let payload = JSON.stringify(body);
  // 发送消息通知
  plus.push.createMessage(msg, payload, options);
  const timer = setTimeout(() => {
    clearTimeout(timer);
    changeBadge();
  }, 1000);
};

// 更新app消息角标
function changeBadge(number) {
  const messages = plus.push.getAllMessage();
  plus.runtime.setBadgeNumber(number || messages.length);
}
